#ifndef NSSHARE_UTILS_FLIP_FLOP_H_
#define NSSHARE_UTILS_FLIP_FLOP_H_

#include "../base/atomic.h"

namespace nsshare
{
namespace utils
{

template<typename ValueType>
class FlipFlop
{
public:
    ValueType Get()
    {
        if(switch_.CompareExchange(1, 0))
            return front_;
        
        ValueType value;
        value = back_;
        switch_.Exchange(0);
        return value;
    }

    void Set(const ValueType & value)
    {
        if(switch_.CompareExchange(1, 0))
        {
            front_ = value;
            return;
        }
        
        back_ = value;
        switch_.Exchange(0);
        return;
    }
private:
    base::Atomic switch_;
    ValueType front_;
    ValueType back_;
}

}
}
#endif